package com.example.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crm.config.MyShiroRealm;
import com.example.crm.entity.Right;
import com.example.crm.entity.Role;
import com.example.crm.entity.User;
import com.example.crm.service.IRightService;
import com.example.crm.service.IRoleService;
import com.example.crm.service.IUserService;
import javax.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zz
 * @since 2024-05-28
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IRightService rightService;
    @RequestMapping("/list")
    public String List(Model model
            ,@RequestParam(value = "roleId",required = false) Integer roleId
            ,@RequestParam(value = "usrName",required = false) String usrName
            ,@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex){
        Page<User> page = new Page<>(pageIndex, 3);
        Page<User> userPager = userService.selectPage(usrName, roleId, page);
        List<Role> roles = roleService.list();
        model.addAttribute("roles",roles);
        model.addAttribute("userPager",userPager);
        model.addAttribute("roleId",roleId);
        return "user/list";
    }

    /*@RequestMapping("/list")
    public String list(Model model, String usrName,
                       @RequestParam(required = false, defaultValue = "0") Long roleId,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex) {
        PageHelper.startPage(pageIndex, 5);
        List<User> users = userService.getAllUsers(usrName, roleId);
        PageInfo<User> userPage = new PageInfo(users);
        List<Role> roleList = roleService.list();
        QueryWrapper<Right> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("right_parent_code", "ROOT_MENU");
        List<Right> right1 = rightService.list(queryWrapper);
        List<Right> right2 = rightService.list();
        model.addAttribute("users", users);
        model.addAttribute("right1", right1);
        model.addAttribute("right2", right2);
        model.addAttribute("roles", roleList);
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        model.addAttribute("userPager", userPage);
        return "user/list";
    }*/

    @RequestMapping("/edit")
    public String edit(@Param("usrId") Long usrId, Model model) {
        User user = userService.getById(usrId);
        List<Role> roles = roleService.list();
        model.addAttribute("roles", roles);
        model.addAttribute("user", user);
        return "user/edit";
    }

    @RequestMapping("/modify")
    public String modify(User user) {
        if (userService.updateById(user)) {
            return "redirect:/user/list";
        }
        return "redirect:/user/edit";
    }

    @DeleteMapping("/doDel")
    @ResponseBody
    public Object doDel(@RequestParam("usrId") Long usrId) {
        Map map = new HashMap();
        try {
            if (userService.del(usrId)) {
                map.put("delResult", "true");
            } else {
                map.put("delResult", "false");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("/add")
    public String add(Model model) {
        List<Role> rolesList = roleService.list();
        model.addAttribute("roles", rolesList);
        return "user/add";
    }

    @PostMapping("/save")
    public String add(User user) {
        userService.save(user);
        return "redirect:/user/list";
    }
}
